package com.syncshot.androidapp.helper;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.google.android.gcm.GCMRegistrar;
import com.syncshot.androidapp.Constant;
import com.syncshot.androidapp.R;
import com.syncshot.androidapp.SyncPhotoApplication;
import com.syncshot.androidapp.helper.WSHelper.Call;
import com.syncshot.androidapp.helper.WSHelper.Call.CallType;
import com.syncshot.androidapp.helper.WSHelper.WebserviceCaller;
import com.syncshot.androidapp.helper.WSHelper.WebserviceCaller.WebserviceCallerListener;
import com.syncshot.androidapp.model.Me;
import com.syncshot.androidapp.model.PushDEventregistration;
import com.syncshot.androidapp.model.PushDRegistration;
import com.syncshot.androidapp.util.WSUtil;


public class NotificationHelper implements WebserviceCallerListener {
	
	private Context mApplicationContext;
	private WeakReference<RegistrationListener> mListener;
	
	 private static class NotificationHelperHolder {
	        private static final NotificationHelper INSTANCE = new NotificationHelper();
	    }

	    /** @return a unique instance of the class */
	    public static NotificationHelper getInstance() {
	        return NotificationHelperHolder.INSTANCE;
	    }
	    
	    public void setApplicationContext(Context context){
	    	mApplicationContext = context;
	    }
	    
	    public void startRegistrationProcess(RegistrationListener listener){
	    	mListener = new WeakReference<NotificationHelper.RegistrationListener>(listener);
	    	WebserviceCaller caller = new WebserviceCaller(this);
			Map<String, Object> parametersMap = new HashMap<String, Object>();
			parametersMap.put("lang", "fr");
			parametersMap.put("badge", "0");
			parametersMap.put("proto", "gcm");
			parametersMap.put("token", GCMRegistrar.getRegistrationId(mApplicationContext));
			
			Call call = new Call(CallType.POST, Constant.PUSH_DIP_ADRESS + "/subscribers", parametersMap, PushDRegistration.class);
			caller.execute(call);
	    }
	    
		@Override
		public void onResult(Call call, Object result) {
			if(result != null && result instanceof PushDRegistration){
				SharedPreferences prefs = mApplicationContext.getSharedPreferences(Constant.PREF, Context.MODE_PRIVATE);
				prefs.edit().putString(Constant.PREF_PUSHD, ((PushDRegistration) result).getmId()).commit();
				Toast.makeText(mApplicationContext, "REGISTRATION OK", Toast.LENGTH_LONG).show();
				if(mListener != null && mListener.get() != null){
					mListener.get().onRegistrationCompleted();
				} else {
					mApplicationContext.sendBroadcast(new Intent(Constant.REGISTERED_BROADCAST));
				}
				//WebserviceCaller ws = new WebserviceCaller(this);
	            /*ws.execute(new Call(CallType.POST, Constant.PUSH_DIP_ADRESS + "/subscriber/" +
				((PushDRegistration) result).getmId() + "/subscriptions/" + Constant.EVENT_ID, null));*/
				Me me = new Me();
	            me.setLatitude(10);
	            me.setLongitude(10);
	            SharedPreferences settings = mApplicationContext.getSharedPreferences(Constant.PREF, 0);
	            me.setUser(settings.getString(Constant.PREF_USERNAME, null));

	            String key = settings.getString(Constant.PREF_PUSHD, "");
	            me.setPushDid(key);
	            (new WebserviceCaller(this)).execute(WSUtil.generateCallUpdateUserLocation(me));

			} else {
				if(result == null || result instanceof PushDEventregistration){
					
				}
			}
			
		}
		
		public boolean isFullyRegistered(){
			if(!GCMRegistrar.getRegistrationId(mApplicationContext).equals("")
					&& !mApplicationContext.getSharedPreferences(Constant.PREF, Context.MODE_PRIVATE).getString(Constant.PREF_PUSHD, "").equals("")){
				return true;
			} else {
				return false;
			}
		}
		
		public interface RegistrationListener{
			
			public void onRegistrationCompleted();
			
		}

}
